#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.const import SubJobTypeEnum


class ActionResult():
    def __init__(self, code, body_err=None, message=None, body_err_params=None):
        self.code = code
        self.body_err = body_err
        self.message = message
        self.body_err_params = body_err_params

    def dict(self, by_alias=False):
        return {"code": self.code,
                "bodyErr": self.body_err,
                "message": self.message,
                "bodyErrParams": [self.body_err_params]
                }


class LogDetail():
    def __init__(self, log_info=None, log_info_param=None, log_detail=None, log_detail_param=None, log_detail_info=None,
                 log_level=None):
        self.log_info = log_info
        self.log_info_param = log_info_param
        self.log_timestamp = None
        self.log_detail = log_detail
        self.log_detail_param = log_detail_param
        self.log_detail_info = log_detail_info
        self.log_level = log_level

    def dict(self, by_alias=False):
        return {"logInfo": self.log_info,
                "logInfoParam": self.log_info_param,
                "logTimestamp": self.log_timestamp,
                "logDetail": self.log_detail,
                "logDetailParam": self.log_detail_param,
                "logDetailInfo": self.log_detail_info,
                "logLevel": self.log_level
                }


class SubJobDetails():
    # 备份进度详情
    def __init__(self, task_id, sub_task_id=None, task_status=None, log_detail=None, progress=None, data_size=None,
                 speed=None, extend_info=None):
        self.task_id = task_id
        self.sub_task_id = sub_task_id
        self.task_status = task_status
        self.log_detail = log_detail
        self.progress = progress
        self.data_size = data_size
        self.speed = speed
        self.extend_info = extend_info

    def dict(self, by_alias=False):
        if not self.log_detail:
            return {"taskId": str(self.task_id),
                    "subTaskId": str(self.sub_task_id),
                    "taskStatus": int(self.task_status),
                    "logDetail": None,
                    "progress": int(self.progress),
                    "dataSize": self.data_size,
                    "speed": self.speed,
                    "extendInfo": self.extend_info
                    }
        return {"taskId": str(self.task_id),
                "subTaskId": str(self.sub_task_id),
                "taskStatus": int(self.task_status),
                "logDetail": [log.dict(by_alias=True) for log in self.log_detail],
                "progress": int(self.progress),
                "dataSize": self.data_size,
                "speed": self.speed,
                "extendInfo": self.extend_info
                }


class SubJobModel():
    """
    生成子任务结构体
    """

    def __init__(self, job_id, job_name, job_priority, policy, ignore_failed, exec_node_id, job_info):
        self.job_id = job_id
        self.job_type = SubJobTypeEnum.BUSINESS_SUB_JOB
        self.job_name = job_name
        self.job_priority = job_priority
        self.policy = policy
        self.ignore_failed = ignore_failed
        self.exec_node_id = exec_node_id
        self.job_info = job_info

    def dict(self, by_alias=False):
        result = {
            "jobId": self.job_id,
            "subJobId": "",
            "jobType": self.job_type,
            "jobName": self.job_name,
            "jobPriority": self.job_priority,
            "policy": self.policy,
            "ignoreFailed": self.ignore_failed,
            "execNodeId": self.exec_node_id,
            "jobInfo": self.job_info,
        }
        return result


class QueryPermissionModel():
    def __init__(self, user, group, file_mode):
        self.user = user
        self.group = group
        self.file_mode = file_mode

    def dict(self, by_alias=True):
        return {"user": self.user,
                "group": self.group,
                "fileMode": self.file_mode
                }


class CopyDataModel():
    def __init__(self, src_obj_path, dst_obj_path, job_id, src_path=None, des_path=None, db_user="informix"):
        self.src_obj_path = src_obj_path
        self.dst_obj_path = dst_obj_path
        self.job_id = job_id
        self.src_path = src_path
        self.des_path = des_path
        self.db_user = db_user

